package com.zjgxzh.userservice.controller;

import cn.dev33.satoken.stp.SaTokenInfo;
import com.zjgxzh.userservice.pojo.User;
import com.zjgxzh.userservice.service.UserService;
import com.zjgxzh.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@CrossOrigin
@RestController
@RequestMapping
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public Result<?> Login(@RequestBody User user) {
        SaTokenInfo tokenInfo = userService.userLogin(user);
        if (tokenInfo != null) {
            System.out.println("登录成功");
            return Result.success(tokenInfo);
        }
        else{
            System.out.println("登录失败");
            return Result.error("用户名或密码错误");
        }
    }

    @PostMapping("/register")
    public Result<?> Register(@RequestBody User user) {
        if(userService.userRegister(user)){
            return Result.success();
        }
        else{
            return Result.error("用户名已存在");
        }
    }

    @GetMapping("/test")
    public Result<?> myTest() {
        return Result.success("测试成功");
    }

}
